define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'vbot/template/index' + location.search,
                    add_url: 'vbot/template/add',
                    edit_url: 'vbot/template/edit',
                    del_url: 'vbot/template/del',
                    multi_url: 'vbot/template/multi',
                    table: 'vbot_template',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'code', title: __('Code')},
                        {
                            field: 'robot_names',
                            title: __('Robot_ids'),
                            formatter: Controller.api.formatter.robot_names,
                            operate: false
                        },
                        {
                            field: 'typelist',
                            title: __('Typelist'),
                            searchList: {
                                "text": __('Typelist text'),
                                "markdown": __('Typelist markdown'),
                                "image": __('Typelist image'),
                                "news": __('Typelist news')
                            },
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'content',
                            title: __('Content'),
                            placeholder: '模糊查找',
                            operate: 'LIKE', 
                            formatter: Controller.api.formatter.content_names
                        },
                        {field: 'title', title: __('Title'), placeholder: '模糊查找', operate: 'LIKE'},
                        {field: 'at_mobiles', title: __('At_mobiles'), operate: 'LIKE', placeholder: '模糊查找'},
                        {
                            field: 'is_atall',
                            title: __('Is_atall'),
                            searchList: {"0": __('Is_atall 0'), "1": __('Is_atall 1')},
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'openswitch',
                            title: __('Openswitch'),
                            searchList: {"0": __('Openswitch 0'), "1": __('Openswitch 1')},
                            formatter: Table.api.formatter.toggle
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'test_msg',
                                    title: __('发送通知消息'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-send-o',
                                    url: 'vbot/template/msg_test',
                                    confirm: '确认发送？',
                                    success: function (data, ret) {
                                        Fast.api.msg(ret.msg);
                                    },
                                    error: function (data, ret) {
                                        Fast.api.msg(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'vbot/template/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'content',
                            title: __('Content'),
                            placeholder: '模糊查找',
                            formatter: Controller.api.formatter.content_names
                        },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'vbot/template/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'vbot/template/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function (e) {
                Form.api.bindevent($("form[role=form]"));

                function need_show(value) {

                    $('.msg_group').hide();

                    if (value == 'text') {
                        $('.msg_content,.msg_at').show(200);
                    } else if (value == 'markdown') {
                        $('.msg_title,.msg_content,.markdown_help').show(200);
                    } else if (value == 'image') {
                        $('.msg_picurl_image').show(200);
                    } else if (value == 'news') {
                        $('.msg_content,.msg_news').show(200);
                    }
                }

                need_show($('#c-typelist')[0].value);

                // 切换消息类型时，显示不同的输入框
                $('#c-typelist').on('change', function () {
                    need_show(this.value);
                })

                $('#show_grammar').on('click', function () {
                    $('.markdown_grammar').show(200);
                    $('.markdown_help').hide();
                })
            },
            formatter: {
                robot_names: function (value, row) {
                    var value = value.split(',');

                    var btn_html = '';

                    for (let i in value) {
                        btn_html += ' <a href="javascript:;" title="' + value[i] + '" class="btn btn-xs btn-info robot_names">' + value[i] + '</a> ';
                    }

                    return btn_html;
                },
                content_names: function (value, row) {
                    if (value.length > 30) {
                        return value.substring(0, 30) + '...';
                    }

                    return value;
                }
            }
        }
    };
    return Controller;
});